<template>
    <el-row style="background-color: #FFFFFF;padding: 10px 0;border-radius: 5px;">
        <el-row style="padding: 10px;margin: 0 10px;">
            <el-row>
                <span class="top-bar">试卷名</span>
                <el-input size="small" style="width: 188px;" v-model="paperQueryDto.name" placeholder="输入处" clearable
                    @clear="handleFilterClear">
                </el-input>
                <span class="top-bar">科目</span>
                <el-select size="small" v-model="paperQueryDto.projectId" placeholder="请选择">
                    <el-option v-for="tag in projectTypeList" :key="tag.id" :label="tag.name" :value="tag.id">
                    </el-option>
                </el-select>
                <span class="top-bar">发布时间</span>
                <el-date-picker size="small" style="margin-left: 10px;width: 220px;" v-model="searchTime"
                    type="daterange" range-separator="至" start-placeholder="开始时间" end-placeholder="结束时间">
                </el-date-picker>
                <el-button size="small" class="customer"
                    style="background-color: rgb(235, 237, 245);color: rgb(43, 121, 203);border: none;" type="primary"
                    @click="handleFilter">立即查询</el-button>
                <el-button size="small" class="customer reset"
                    style="background-color: #f1f1f1;border: none;color: #909399;border: 1px solid #f1f1f1;" type="info"
                    @click="resetQueryCondition">重置</el-button>
            </el-row>
        </el-row>
        <el-row style="margin: 10px 20px;">
            <el-table :data="tableData" style="width: 100%">
                <el-table-column prop="userName" width="88" label="发布者"> </el-table-column>
                <el-table-column prop="name" width="288" label="试卷名"></el-table-column>
                <el-table-column prop="projectName" width="138" label="科目"> </el-table-column>
                <el-table-column prop="isShow" width="148" label="审核状态">
                    <template slot-scope="scope">
                        <span>{{ scope.row.isShow ? '已审核' : '未审核' }}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="limitTime" width="100" label="限时(min)"> </el-table-column>
                <el-table-column prop="totalScore" width="100" label="总分"> </el-table-column>
                <el-table-column prop="createTime" width="168" label="发布时间"></el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <span class="text-button" @click="answerPaper(scope.row)">答题</span>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination style="margin: 20px 0;float: right;" @size-change="handleSizeChange"
                @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[10, 20]"
                :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
                :total="totalItems"></el-pagination>
        </el-row>
    </el-row>
</template>

<script>
export default {
    data() {
        return {
            data: { cover: '' },
            filterText: '',
            currentPage: 1,
            pageSize: 10,
            totalItems: 0,
            tableData: [],
            searchTime: [],
            paperQueryDto: {}, // 搜索条件
            projectTypeList: [],
            practiceTypes: [],
        };
    },
    created() {
        this.fetchFreshData();
        this.loadAllProjectType();
    },
    methods: {
        answerPaper(paper) {
            //跳之前，先存储卷子信息
            sessionStorage.setItem('paperInfo', JSON.stringify(paper));
            // 跳到答题页面
            this.$router.push('/answerPaper');
        },
        onReceiveContent(html) {
            this.data.detail = html;
        },
        // 加载全部的试卷数据
        loadAllProjectType() {
            this.$axios.post(`/project-type/query`, {}).then(response => {
                const { data } = response;
                if (data.code === 200) {
                    this.projectTypeList = data.data;
                }
            })
        },
        resetQueryCondition() {
            this.paperQueryDto = {};
            this.searchTime = [];
            this.fetchFreshData();
        },

        async fetchFreshData() {
            try {
                this.tableData = [];
                let startTime = null;
                let endTime = null;
                if (this.searchTime != null && this.searchTime.length === 2) {
                    const [startDate, endDate] = await Promise.all(this.searchTime.map(date => date.toISOString()));
                    startTime = `${startDate.split('T')[0]}T00:00:00`;
                    endTime = `${endDate.split('T')[0]}T23:59:59`;
                }
                // 请求参数
                const params = {
                    current: this.currentPage,
                    size: this.pageSize,
                    startTime: startTime,
                    endTime: endTime,
                    ...this.paperQueryDto
                };
                const response = await this.$axios.post('/paper/query', params);
                const { data } = response;
                this.tableData = data.data;
                this.totalItems = data.total;
            } catch (error) {
                console.error('查询试卷信息异常:', error);
            }
        },
        handleFilter() {
            this.currentPage = 1;
            this.fetchFreshData();
        },
        handleFilterClear() {
            this.filterText = '';
            this.handleFilter();
        },
        handleSizeChange(val) {
            this.pageSize = val;
            this.currentPage = 1;
            this.fetchFreshData();
        },
        handleCurrentChange(val) {
            this.currentPage = val;
            this.fetchFreshData();
        },
    },
};
</script>
<style scoped lang="scss">
.tag-tip {
    display: inline-block;
    padding: 5px 10px;
    border-radius: 5px;
    background-color: rgb(245, 245, 245);
    color: rgb(104, 118, 130);
}

.input-def {
    height: 40px;
    line-height: 40px;
    outline: none;
    border: none;
    font-size: 20px;
    color: rgb(102, 102, 102);
    font-weight: 900;
    width: 100%;
}

.dialog-footer {
    /* 使按钮水平居中 */
    display: flex;
    justify-content: center;
    align-items: center;
}

/* 如果需要调整按钮之间的间距 */
.customer {
    margin: 0 8px;
    /* 根据需要调整间距 */
}
</style>